﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ThermalEquipmentDesign.Model.SmoothPipeR717R22
{
    public class SmoothPipeR717R22Model
    {
        public SmoothPipeR717R22Result result = new SmoothPipeR717R22Result();
        public SmoothPipeR717R22 smoothpiper717r22;

        /// <summary>
        /// Calculate Water Temperature In
        /// </summary>
        /// <returns>Water Temperature In</returns>
        private double WaterTemperatureIn()
        {
            double tb1 = smoothpiper717r22.DbwaterTemperatureOut + 4;
            return tb1;
        }

        /// <summary>
        /// Calculate Boiling Temperature
        /// </summary>
        /// <returns>Boiling Temperature</returns>
        private double BoilingTemperature()
        {
            double to = smoothpiper717r22.DbwaterTemperatureOut - 5;
            return to;
        }

        /// <summary>
        /// Calculate Average Temperature Logarit
        /// </summary>
        /// <returns>Average Temperature Logarit</returns>
        private double AverageTemperatureLogarit()
        {
            double tb1 = smoothpiper717r22.DbwaterTemperatureOut + 4;
            double to = smoothpiper717r22.DbwaterTemperatureOut - 5;
            double tmax = tb1 - to;
            double ttb = (tmax - 5) / (Math.Log(tmax / 5) / Math.Log(Math.E));
            return ttb;
        }

        /// <summary>
        /// Calculate Number Tubes In Way
        /// </summary>
        /// <returns>Number Tubes In Way</returns>
        private double NumberTubesInWay()
        {
            double n1 = 4 * smoothpiper717r22.Dbcapacity / (1.5 * Math.Pow(smoothpiper717r22.Dbdiameter, 2) * 2.91 * 4 * 1232);
            return Math.Round(n1,1);
        }

        /// <summary>
        /// Calculate Water Speed
        /// </summary>
        /// <returns>Water Speed</returns>
        private double WaterSpeed()
        {
            double n1 = 4 * smoothpiper717r22.Dbcapacity / (1.5 * Math.Pow(smoothpiper717r22.Dbdiameter, 2) * 2.91 * 4 * 1232);
            double w = 4 * smoothpiper717r22.Dbcapacity / (n1 * Math.Pow(smoothpiper717r22.Dbdiameter, 2) * 2.91 * 4 * 1232);
            return w;
        }

        /// <summary>
        /// Calculate Reynolds Value
        /// </summary>
        /// <returns>Reynolds Value</returns>
        private double ReynoldsValue()
        {
            double n1 = 4 * smoothpiper717r22.Dbcapacity / (1.5 * Math.Pow(smoothpiper717r22.Dbdiameter, 2) * 2.91 * 4 * 1232);
            double w = 4 * smoothpiper717r22.Dbcapacity / (n1 * Math.Pow(smoothpiper717r22.Dbdiameter, 2) * 2.91 * 4 * 1232);
            double Re = w * smoothpiper717r22.Dbdiameter / (4.374 * Math.Pow(10, -6));
            return Re;
        }

        /// <summary>
        /// Calculate Nusselt Value
        /// </summary>
        /// <returns>Nusselt Value</returns>
        private double NusseltValue()
        {
            double n1 = 4 * smoothpiper717r22.Dbcapacity / (1.5 * Math.Pow(smoothpiper717r22.Dbdiameter, 2) * 2.91 * 4 * 1232);
            double w = 4 * smoothpiper717r22.Dbcapacity / (n1 * Math.Pow(smoothpiper717r22.Dbdiameter, 2) * 2.91 * 4 * 1232);
            double Re = w * smoothpiper717r22.Dbdiameter / (4.374 * Math.Pow(10, -6));
            double Nu = 0.023 * Math.Pow(Re, 0.8) * Math.Pow(29.56, 0.33);
            return Nu;
        }

        /// <summary>
        /// Calculate Heat Transfer Coefficient
        /// </summary>
        /// <returns>Heat Transfer Coefficient</returns>
        private double HeatTransferCoefficient()
        {
            double n1 = 4 * smoothpiper717r22.Dbcapacity / (1.5 * Math.Pow(smoothpiper717r22.Dbdiameter, 2) * 2.91 * 4 * 1232);
            double w = 4 * smoothpiper717r22.Dbcapacity / (n1 * Math.Pow(smoothpiper717r22.Dbdiameter, 2) * 2.91 * 4 * 1232);
            double Re = w * smoothpiper717r22.Dbdiameter / (4.374 * Math.Pow(10, -6));
            double Nu = 0.023 * Math.Pow(Re, 0.8) * Math.Pow(29.56, 0.33);
            double abi = Nu * 0.526 / smoothpiper717r22.Dbdiameter;
            return Math.Round(abi,0);
        }

        /// <summary>
        /// Calculate Density Water Salt
        /// </summary>
        /// <returns>Density Water Salt</returns>
        private double DensityWaterSalt()
        {
            double n1 = 4 * smoothpiper717r22.Dbcapacity / (1.5 * Math.Pow(smoothpiper717r22.Dbdiameter, 2) * 2.91 * 4 * 1232);
            double w = 4 * smoothpiper717r22.Dbcapacity / (n1 * Math.Pow(smoothpiper717r22.Dbdiameter, 2) * 2.91 * 4 * 1232);
            double Re = w * smoothpiper717r22.Dbdiameter / (4.374 * Math.Pow(10, -6));
            double Nu = 0.023 * Math.Pow(Re, 0.8) * Math.Pow(29.56, 0.33);
            double abi = Nu * 0.526 / smoothpiper717r22.Dbdiameter;
            double qFb = smoothpiper717r22.DbDiviateTemperatureWaterSalt / (1 / abi + 0.0008);
            return qFb;
        }

        /// <summary>
        /// Calculate Density Heat NH3
        /// </summary>
        /// <returns>Density Heat NH3</returns>
        private double DensityHeatNH3()
        {
            double qNH3 = 580 * Math.Pow(smoothpiper717r22.DbDiviateBoilingTemperature, 1.667) * smoothpiper717r22.DbouterDiameter / smoothpiper717r22.Dbdiameter;
            return qNH3;
        }

        /// <summary>
        /// Calculate Density Heat R22
        /// </summary>
        /// <returns>Density Heat R22</returns>
        private double DensityHeatR22()
        {
            double qR22 = 4.47 * Math.Pow(0.2603, 4) * Math.Pow(3, 0.8) * 1.7 * Math.Pow(smoothpiper717r22.DbDiviateBoilingTemperature, 4) * smoothpiper717r22.DbouterDiameter / smoothpiper717r22.Dbdiameter;
            return qR22;
        }

        /// <summary>
        /// Calculate Heat Exchange Area NH3
        /// </summary>
        /// <returns>Heat Exchange Area NH3</returns>
        private double HeatExchangeAreaNH3()
        {
            double qNH3 = 580 * Math.Pow(smoothpiper717r22.DbDiviateBoilingTemperature, 1.667) * smoothpiper717r22.DbouterDiameter / smoothpiper717r22.Dbdiameter;
            double FNH3 = smoothpiper717r22.Dbcapacity / qNH3;
            return FNH3;
        }

        /// <summary>
        /// Calculate Heat Exchange Area R22
        /// </summary>
        /// <returns>Heat Exchange Area R22</returns>
        private double HeatExchangeAreaR22()
        {
            double qR22 = 4.47 * Math.Pow(0.2603, 4) * Math.Pow(3, 0.8) * 1.7 * Math.Pow(smoothpiper717r22.DbDiviateBoilingTemperature, 4) * smoothpiper717r22.DbouterDiameter / smoothpiper717r22.Dbdiameter;
            double FR22 = smoothpiper717r22.Dbcapacity / qR22;
            return FR22;
        }

        /// <summary>
        /// Calculate Buoc Ong
        /// </summary>
        /// <returns>Segment Tube</returns>
        private double BuocOng()
        {
            double BO = 1.3 * smoothpiper717r22.DbouterDiameter;
            return BO;
        }

        /// <summary>
        /// Calculate Tubes Layout NH3
        /// </summary>
        /// <returns>Tubes Layout NH3</returns>
        private double TubesLayoutNH3()
        {
            double qNH3 = 580 * Math.Pow(smoothpiper717r22.DbDiviateBoilingTemperature, 1.667) * smoothpiper717r22.DbouterDiameter / smoothpiper717r22.Dbdiameter;
            double FNH3 = smoothpiper717r22.Dbcapacity / qNH3;
            double BO = 1.3 * smoothpiper717r22.DbouterDiameter;
            double nNH3 = 0.75*Math.Pow(FNH3/(smoothpiper717r22.Dbdiameter*7*BO), 1/3);
            return nNH3;
        }

        /// <summary>
        /// Calculate Tubes Layout R22
        /// </summary>
        /// <returns>Tubes Layout R22</returns>
        private double TubesLayoutR22()
        {
            double qR22 = 4.47 * Math.Pow(0.2603, 4) * Math.Pow(3, 0.8) * 1.7 * Math.Pow(smoothpiper717r22.DbDiviateBoilingTemperature, 4) * smoothpiper717r22.DbouterDiameter / smoothpiper717r22.Dbdiameter;
            double FR22 = smoothpiper717r22.Dbcapacity / qR22;
            double BO = 1.3 * smoothpiper717r22.DbouterDiameter;
            double nR22 = 0.75 * Math.Pow(FR22 / (smoothpiper717r22.Dbdiameter * 7 * BO), 1 / 3);
            return nR22;
        }

        /// <summary>
        /// Calculate Casing Diameter
        /// </summary>
        /// <returns>Casing Diameter</returns>
        private double CasingDiameter()
        {
            double qR22 = 4.47 * Math.Pow(0.2603, 4) * Math.Pow(3, 0.8) * 1.7 * Math.Pow(smoothpiper717r22.DbDiviateBoilingTemperature, 4) * smoothpiper717r22.DbouterDiameter / smoothpiper717r22.Dbdiameter;
            double FR22 = smoothpiper717r22.Dbcapacity / qR22;
            double BO = 1.3 * smoothpiper717r22.DbouterDiameter;
            double nR22 = 0.75 * Math.Pow(FR22 / (smoothpiper717r22.Dbdiameter * 7 * BO), 1 / 3);
            double qNH3 = 580 * Math.Pow(smoothpiper717r22.DbDiviateBoilingTemperature, 1.667) * smoothpiper717r22.DbouterDiameter / smoothpiper717r22.Dbdiameter;
            double FNH3 = smoothpiper717r22.Dbcapacity / qNH3;
            double nNH3 = 0.75 * Math.Pow(FNH3 / (smoothpiper717r22.Dbdiameter * 7 * BO), 1 / 3);
            double n = (nNH3 + nR22)/2;
            double Di = n * BO;
            return Math.Round(Di,3);
        }

        /// <summary>
        /// Calculate Length Casing
        /// </summary>
        /// <returns>Length Casing</returns>
        private double LengthCasing()
        {
            double qR22 = 4.47 * Math.Pow(0.2603, 4) * Math.Pow(3, 0.8) * 1.7 * Math.Pow(smoothpiper717r22.DbDiviateBoilingTemperature, 4) * smoothpiper717r22.DbouterDiameter / smoothpiper717r22.Dbdiameter;
            double FR22 = smoothpiper717r22.Dbcapacity / qR22;
            double BO = 1.3 * smoothpiper717r22.DbouterDiameter;
            double nR22 = 0.75 * Math.Pow(FR22 / (smoothpiper717r22.Dbdiameter * 7 * BO), 1 / 3);
            double qNH3 = 580 * Math.Pow(smoothpiper717r22.DbDiviateBoilingTemperature, 1.667) * smoothpiper717r22.DbouterDiameter / smoothpiper717r22.Dbdiameter;
            double FNH3 = smoothpiper717r22.Dbcapacity / qNH3;
            double nNH3 = 0.75 * Math.Pow(FNH3 / (smoothpiper717r22.Dbdiameter * 7 * BO), 1 / 3);
            double n = (nNH3 + nR22) / 2;
            double Di = n * BO;
            double l = 7 * Di;
            return Math.Round(l,3);
        }

        /// <summary>
        /// Calculate Total Tube NH3
        /// </summary>
        /// <returns>Total Tube NH3</returns>
        private double TotalTubeNH3()
        {
            double qR22 = 4.47 * Math.Pow(0.2603, 4) * Math.Pow(3, 0.8) * 1.7 * Math.Pow(smoothpiper717r22.DbDiviateBoilingTemperature, 4) * smoothpiper717r22.DbouterDiameter / smoothpiper717r22.Dbdiameter;
            double FR22 = smoothpiper717r22.Dbcapacity / qR22;
            double BO = 1.3 * smoothpiper717r22.DbouterDiameter;
            double nR22 = 0.75 * Math.Pow(FR22 / (smoothpiper717r22.Dbdiameter * 7 * BO), 1 / 3);
            double qNH3 = 580 * Math.Pow(smoothpiper717r22.DbDiviateBoilingTemperature, 1.667) * smoothpiper717r22.DbouterDiameter / smoothpiper717r22.Dbdiameter;
            double FNH3 = smoothpiper717r22.Dbcapacity / qNH3;
            double nNH3 = 0.75 * Math.Pow(FNH3 / (smoothpiper717r22.Dbdiameter * 7 * BO), 1 / 3);
            double n = (nNH3 + nR22) / 2;
            double Di = n * BO;
            double l = 7 * Di;
            double TotalNH3 = FNH3 / (Math.PI * smoothpiper717r22.Dbdiameter * l);
            return Math.Round(TotalNH3,0);
        }

        /// <summary>
        /// Calculate Total Tube R22
        /// </summary>
        /// <returns>Total Tube R22</returns>
        private double TotalTubeR22()
        {
            double qR22 = 4.47 * Math.Pow(0.2603, 4) * Math.Pow(3, 0.8) * 1.7 * Math.Pow(smoothpiper717r22.DbDiviateBoilingTemperature, 4) * smoothpiper717r22.DbouterDiameter / smoothpiper717r22.Dbdiameter;
            double FR22 = smoothpiper717r22.Dbcapacity / qR22;
            double BO = 1.3 * smoothpiper717r22.DbouterDiameter;
            double nR22 = 0.75 * Math.Pow(FR22 / (smoothpiper717r22.Dbdiameter * 7 * BO), 1 / 3);
            double qNH3 = 580 * Math.Pow(smoothpiper717r22.DbDiviateBoilingTemperature, 1.667) * smoothpiper717r22.DbouterDiameter / smoothpiper717r22.Dbdiameter;
            double FNH3 = smoothpiper717r22.Dbcapacity / qNH3;
            double nNH3 = 0.75 * Math.Pow(FNH3 / (smoothpiper717r22.Dbdiameter * 7 * BO), 1 / 3);
            double n = (nNH3 + nR22) / 2;
            double Di = n * BO;
            double l = 7 * Di;
            double TotalR22 = FR22 / (Math.PI * smoothpiper717r22.Dbdiameter * l);
            return Math.Round(TotalR22,0);
        }

        /// <summary>
        /// Calculate Ways R22
        /// </summary>
        /// <returns>Ways R22</returns>
        private double WaysR22()
        {
            double qR22 = 4.47 * Math.Pow(0.2603, 4) * Math.Pow(3, 0.8) * 1.7 * Math.Pow(smoothpiper717r22.DbDiviateBoilingTemperature, 4) * smoothpiper717r22.DbouterDiameter / smoothpiper717r22.Dbdiameter;
            double FR22 = smoothpiper717r22.Dbcapacity / qR22;
            double BO = 1.3 * smoothpiper717r22.DbouterDiameter;
            double nR22 = 0.75 * Math.Pow(FR22 / (smoothpiper717r22.Dbdiameter * 7 * BO), 1 / 3);
            double qNH3 = 580 * Math.Pow(smoothpiper717r22.DbDiviateBoilingTemperature, 1.667) * smoothpiper717r22.DbouterDiameter / smoothpiper717r22.Dbdiameter;
            double FNH3 = smoothpiper717r22.Dbcapacity / qNH3;
            double nNH3 = 0.75 * Math.Pow(FNH3 / (smoothpiper717r22.Dbdiameter * 7 * BO), 1 / 3);
            double n = (nNH3 + nR22) / 2;
            double Di = n * BO;
            double l = 7 * Di;
            double TotalR22 = FR22 / (Math.PI * smoothpiper717r22.Dbdiameter * l);
            double zR22 = TotalR22 / nR22;
            return Math.Round(zR22,0);
        }

        /// <summary>
        /// Calculate Ways NH3
        /// </summary>
        /// <returns>Ways NH3</returns>
        private double WaysNH3()
        {
            double qR22 = 4.47 * Math.Pow(0.2603, 4) * Math.Pow(3, 0.8) * 1.7 * Math.Pow(smoothpiper717r22.DbDiviateBoilingTemperature, 4) * smoothpiper717r22.DbouterDiameter / smoothpiper717r22.Dbdiameter;
            double FR22 = smoothpiper717r22.Dbcapacity / qR22;
            double BO = 1.3 * smoothpiper717r22.DbouterDiameter;
            double nR22 = 0.75 * Math.Pow(FR22 / (smoothpiper717r22.Dbdiameter * 7 * BO), 1 / 3);
            double qNH3 = 580 * Math.Pow(smoothpiper717r22.DbDiviateBoilingTemperature, 1.667) * smoothpiper717r22.DbouterDiameter / smoothpiper717r22.Dbdiameter;
            double FNH3 = smoothpiper717r22.Dbcapacity / qNH3;
            double nNH3 = 0.75 * Math.Pow(FNH3 / (smoothpiper717r22.Dbdiameter * 7 * BO), 1 / 3);
            double n = (nNH3 + nR22) / 2;
            double Di = n * BO;
            double l = 7 * Di;
            double TotalNH3 = FNH3 / (Math.PI * smoothpiper717r22.Dbdiameter * l);
            double zNH3 = TotalNH3 / nNH3;
            return Math.Round(zNH3,0);
        }

        /// <summary>
        /// Calculate Smooth Pipe R717 R22
        /// </summary>
        /// /// <param name="con">SmoothPipeR717R22 object</param>
        /// <returns>SmoothPipeR717R22Result object</returns>
        public SmoothPipeR717R22Result ImplementCalculation(SmoothPipeR717R22 spR717R22)
        {
            smoothpiper717r22 = spR717R22;
            result.DbAverageTemperatureLogarit = AverageTemperatureLogarit();
            result.DbBoilingTemperature = BoilingTemperature();
            result.DbBuocOng = BuocOng();
            result.DbCasingDiameter = CasingDiameter();
            result.DbDensityHeatNH3 = DensityHeatNH3();
            result.DbDensityHeatR22 = DensityHeatR22();
            result.DbDensityWaterSalt = DensityWaterSalt();
            result.DbHeatExchangeAreaNH3 = HeatExchangeAreaNH3();
            result.DbHeatExchangeAreaR22 = HeatExchangeAreaR22();
            result.DbHeatTransferCoefficient = HeatTransferCoefficient();
            result.DbLengthCasing = LengthCasing();
            result.DbNumberTubesInWay = NumberTubesInWay();
            result.DbNusseltValue = NusseltValue();
            result.DbReynoldsValue = ReynoldsValue();
            result.DbTotalTubeNH3 = TotalTubeNH3();
            result.DbTotalTubeR22 = TotalTubeR22();
            result.DbTubesLayoutNH3 = TubesLayoutNH3();
            result.DbTubesLayoutR22 = TubesLayoutR22();
            result.DbWaterSpeed = WaterSpeed();
            result.DbWaterTemperatureIn = WaterTemperatureIn();
            result.DbWaysNH3 = WaysNH3();
            result.DbWaysR22 = WaysR22();
            return result;
        }
    }
}
